from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:

    def flatten(self, root: Optional[TreeNode]) -> None:
        if root is None:
            return
        self.flatten(root.right)
        self.flatten(root.left)

        left = root.left
        right = root.right

        root.left = None
        root.right = left
        p = root
        while p.right is not None:
            p = p.right
        p.right = right
        
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    root = TreeNode.create_root([1,2,5,3,4,None,6])
    a=solution.flatten(root)
    print(a)
    # your test code here